|
|
|
|
|
|
|
The QueryDef object contains information about a stored SQL query. SQL queries can be used as record sources for the Visual Basic data control or as the first parameter in the Recordset object. QueryDef objects run faster than inline SQL queries, because Visual Basic must go through a processing step before executing an SQL query. Stored queries (QueryDef objects) are stored in their processed format. Using QueryDef objects means there is one less processing step to go through before you see your data. |
|
|
|
|
|
|
|
|
The example in Listing 9.20 creates a simple SELECT SQL query and stores it for later use. After creating the query, you apply it as a record source when creating a Recordset object. Finally, you enumerate the QueryDef properties. Add another button with its Name property set to cmdQuery and its Caption property set to &Query. Add the code in Listing 9.20 to the cmdQuery_Click code window. |
|
|
|
|
|
|
|
|
Listing 9.20. Coding the cmdQuery_Click event. |
|
|
|
|
|
|
|
|
Private Sub cmdQueryDef_Click()
'
' create a stored query
'
On Error Resume Next
'
Dim ws As Workspace
Dim db As Database
Dim qd As QueryDef
Dim pr As Property
'
Dim strDBName As String
Dim strQDName As String
Dim strQDSQL As String
Dim strMsg As String
'
' init vars
strDBName = App.Path & "\NewDB.mdb"
strQDName = "qryNewQuery"
strQDSQL = "SELECT * FROM NewTable WHERE NewField<>NULL"
'
' create db & table
cmdCreateTable_Click
'
' open ws and db
Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(strDBName)
'
' create a new query
Set qd = db.CreateQueryDef(strQDName)
qd.SQL = strQDSQL
' |
|
|
|
|
|